﻿using System;
using System.Text;
using System.Web;
using TidyBlog.Util;
using TidyBlog.Entity;
using TidyBlog.DAL;

namespace TidyBlog.BLL
{
    /// <summary>
    /// 网站设置业务逻辑层
    /// </summary>
    public class SetupBLL
    {
        /// <summary>
        /// 缓存键名
        /// </summary>
        public const string CacheName = "Setup";
        
        /// <summary>
        /// 验证网站设置参数是否合法
        /// </summary>
        /// <param name="setup">网站设置</param>
        public static void Validate(Setup setup)
        {
            if (setup.IsOpen != 1 && setup.IsOpen != 0)
            {
                throw new ArgumentException("网站开放参数错误");
            }
            if (String.IsNullOrEmpty(setup.SiteName))
            {
                throw new ArgumentException("网站名不能为空");
            }
            if (String.IsNullOrEmpty(setup.SiteDomain))
            {
                throw new ArgumentException("网站域名不能为空");
            }
            if (!String.IsNullOrEmpty(setup.ForbiddenIP))
            {
                var ips = setup.ForbiddenIP.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var ip in ips)
                {
                    if (!Validator.IsIP(ip))
                    {
                        throw new ArgumentException(ip + " 不是合法的IP地址");
                    }
                }
                setup.ForbiddenIP = String.Join(Environment.NewLine, ips);
            }
            if (setup.CommentLimit != 0 && setup.CommentLimit != 1 && setup.CommentLimit != 2)
            {
                throw new ArgumentException("评论开放参数错误");
            }
            if (setup.MinCommentSize > setup.MaxCommentSize)
            {
                throw new ArgumentException("最小评论字数不能大于最大评论字数");
            }
        }

        /// <summary>
        /// 读取网站设置
        /// </summary>
        /// <returns>网站设置</returns>
        public static Setup Read()
        {
            var result = HttpContext.Current.Cache[CacheName] as Setup;

            if (result == null)
            {
                try
                {
                    result = SetupDAL.Read();
                }
                catch
                {
#if DEBUG
                    throw;
#else
                    throw new Exception("读取网站设置记录出错");
#endif
                }

                HttpContext.Current.Cache[CacheName] = result;
            }

            return (Setup)result.Clone();
        }

        /// <summary>
        /// 更新网站设置
        /// </summary>
        /// <param name="setup">网站设置</param>
        public static void Update(Setup setup)
        {
            Validate(setup);

            try
            {
                SetupDAL.Update(setup);
            }
            catch
            {
#if DEBUG
                throw;
#else
                throw new Exception("更新网站设置记录出错");
#endif
            }

            HttpContext.Current.Cache.Remove(CacheName);
        }
    }
}